/////////////////////////////////////////////////////////////////////////////
// Name:        variantstream.cpp
// Purpose:     Helper class for using variants in streams
// Author:      Julian Smart
// Modified by: 
// Created:     2009-02-27
// RCS-ID:      
// Copyright:   (c) Julian Smart
// License:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

#include "wx/datstrm.h"
#include "wx/wfstream.h"

#include "variantutils/varianthandlers.h"
#include "variantutils/variantstream.h"

/*
 * A neater way of reading and writing attributes
 */

void wxDataStreamHelper::Init()
{
    m_outputStream = NULL;
    m_outputStream = NULL;
    
    m_inputStream = NULL;
    m_dataInputStream = NULL;
}

bool wxDataStreamHelper::Read(wxVariant& variant)
{
    wxASSERT(m_inputStream != NULL);
    wxASSERT(m_dataInputStream != NULL);
    if (!m_inputStream || !m_dataInputStream)
        return false;

    if (wxReadVariant(variant, *m_inputStream, *m_dataInputStream))
    {
        if (variant.GetName() == wxT("*END*") && variant.GetType() == wxT("string") && variant.GetString() == wxT("*END*"))
            return false;
        else
            return true;
    }
    else
        return false;
}

bool wxDataStreamHelper::Write(const wxVariant& value)
{
    wxASSERT(m_outputStream != NULL);
    wxASSERT(m_dataOutputStream != NULL);
    if (!m_outputStream || !m_dataOutputStream)
        return false;

    return wxWriteVariant(value, *m_outputStream, *m_dataOutputStream);
}

bool wxDataStreamHelper::Write(const wxString& value, const wxString& name)
{
    return Write(wxVariant(value, name));
}

bool wxDataStreamHelper::WriteIfNotEmpty(const wxString& value, const wxString& name)
{
    if (!value.IsEmpty())
    {
        return Write(wxVariant(value, name));
    }
    return true;
}

bool wxDataStreamHelper::Write(double value, const wxString& name)
{
    return Write(wxVariant(value, name));
}

bool wxDataStreamHelper::Write(long value, const wxString& name)
{
    return Write(wxVariant(value, name));
}

bool wxDataStreamHelper::Write(int value, const wxString& name)
{
    return Write(wxVariant(value, name));
}

bool wxDataStreamHelper::Write(bool value, const wxString& name)
{
    return Write(wxVariant(value, name));
}

bool wxDataStreamHelper::WriteEnd()
{
    static wxVariant s_varEnd(wxT("*END*"), wxT("*END*"));
    return Write(s_varEnd);
}
